package com.example.panpan.controller;

import com.example.panpan.Service.GoodsCategoryService;
import com.example.panpan.domain.GoodsCategoryEntity;
import com.example.panpan.domain.PageResult;
import com.example.panpan.domain.utils.ResponseWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


public class GoodsCategoryController {
    private Logger log = LoggerFactory.getLogger(GoodsCategoryController.class);

    @Resource(name = "goodsCategoryServiceImpl")
    private GoodsCategoryService goodsCategoryService;

    /**
     * 获取goodsCategory表数据
     *
     * @param pageSize
     * @param page
     * @return
     */
    @GetMapping("/goodsCategory")
    public ResponseWrapper<PageResult> goodsCategoryList( int pageSize, int page, String categoryId) {
        PageResult pageResult = new PageResult();
        pageResult.setData(goodsCategoryService.goodsCategoryList(pageSize, page * pageSize,categoryId));
        pageResult.setTotalCount(goodsCategoryService.goodsCategorySize(pageSize, page * pageSize,categoryId));
        log.debug("The method is ending");
        return new ResponseWrapper<>(pageResult);
    }

    /**
     * 通过parentId得到menus列表
     *
     * @param parentId
     * @return
     */
    @GetMapping("/goodaCategory/parentId")
    public ResponseWrapper<List<GoodsCategoryEntity>> goodsCategoryByParentId(int parentId) {
        return new ResponseWrapper<>(goodsCategoryService.goodsCategoryByParentId(parentId));
    }

    /**
     * 新建角色信息
     *
     * @param goodsCategoryEntity
     * @return
     */
    @PostMapping("/goodsCategory/category")
    public ResponseWrapper<GoodsCategoryEntity> insertGoodsCategory(@RequestBody GoodsCategoryEntity goodsCategoryEntity) {
        goodsCategoryService.insertGoodsCategory(goodsCategoryEntity);
        log.debug("The method is ending");
        return new ResponseWrapper<>(goodsCategoryEntity);
    }

    /**
     * 更新角色信息
     *
     * @param goodsCategoryEntity
     * @param id
     * @return
     */
    @PutMapping("/goodsCategory/{id}")
    public ResponseWrapper<GoodsCategoryEntity> updateGoodsCategory(@RequestBody GoodsCategoryEntity goodsCategoryEntity, @PathVariable int id) {
        if (goodsCategoryEntity.getId() == id) {
            goodsCategoryService.updateGoodsCategory(goodsCategoryEntity);
        }
        log.debug("The method is ending");
        return new ResponseWrapper<>(goodsCategoryEntity);
    }

    /**
     * 删除角色信息
     *
     * @param groupId
     * @return
     */
    @DeleteMapping("/goodsCategory")
    public ResponseWrapper<List<String>> deleteGoodsCategory(@RequestBody List<String> groupId) {
        goodsCategoryService.deleteGoodsCategory(groupId);
        return new ResponseWrapper<>(groupId);
    }
}
